//
//  MILRegisterViewController.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/12.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "MILRegisterViewController.h"
#import "GetRegisterCodeApi.h"
#import "RegisterApi.h"

@interface MILRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myUserNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *myRegisterCoderTextField;
@property (weak, nonatomic) IBOutlet UITextField *myPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *registerCoderBtn;
@property(nonatomic,assign)NSInteger temptime;//获得验证码后时间倒数
@property (nonatomic , strong) NSTimer *TimerWithTimerout;
@end

@implementation MILRegisterViewController

#pragma mark -- UI
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
//    [self hideNavigationBottomLine];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshBaseControlValue
{
    [self setNavigationBarTitle:@"注册"];
    self.navigationBar.backgroundColor = [UIColor blackColor];
    [self setLeftButtonImage:[UIImage imageNamed:@"back_icon"]];
    self.view.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    
}

#pragma mark -- 按钮点击事件
//获取验证码
- (IBAction)getRegisterCoderAction:(UIButton *)sender {
    if (!self.myUserNameTextField.text || [self.myUserNameTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空！"];
        return;
    }
    
    if (![self.myUserNameTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    //获取验证码
    //调用获取验证码接口
    GetRegisterCodeApi *api = [[GetRegisterCodeApi alloc]initWithDic:@{@"username":self.myUserNameTextField.text,@"type":@(1)}];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary * dict = [api responseJSONObject];
        NSLog(@"dict = %@",dict[@"errmsg"]);
        if ([dict[@"errcode"] intValue] == 0) {
            
            [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];
            self.temptime = 60;
            [self.registerCoderBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [self.registerCoderBtn setTitle:[NSString stringWithFormat:@"%lds",(long)self.temptime] forState:UIControlStateNormal];
            self.registerCoderBtn.enabled = NO;
            [self.TimerWithTimerout invalidate];
            self.TimerWithTimerout = nil;
            self.TimerWithTimerout = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                      target:self selector:@selector(VersionTimerout)
                                                                    userInfo:nil
                                                                     repeats:YES];
            
        } else {
            [SVProgressHUD showErrorWithStatus:dict[@"errmsg"]];
            [self.TimerWithTimerout invalidate];
            self.TimerWithTimerout = nil;
            self.registerCoderBtn.enabled = YES;
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];
        [self.TimerWithTimerout invalidate];
        self.TimerWithTimerout = nil;
        NSString *dic=[request.error description];
        NSLog(@"YTKBaseRequest====%@",dic);
        self.registerCoderBtn.enabled = YES;
    }];

}

//立即注册
- (IBAction)myRegisterBtnAction:(UIButton *)sender {
    //调用注册的接口
    if (!self.myUserNameTextField.text || [self.myUserNameTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空！"];
        return;
    }
    if (!self.myRegisterCoderTextField.text || [self.myRegisterCoderTextField.text isEqualToString:@""] ) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空！"];
        return;
    }
    if (!self.myPasswordTextField.text || [self.myPasswordTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空！"];
        return;
    }
    if (self.myPasswordTextField.text.length>16 || self.myPasswordTextField.text.length<6) {
        [SVProgressHUD showErrorWithStatus:@"密码长度应该在6-16位之间"];
        return;
    }
    
    if (![self.myUserNameTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (![self.myPasswordTextField.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-16位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    [self registerActionStart];

}

- (void)registerActionStart{
    
    RegisterApi *registerApi = [[RegisterApi alloc]initWithDic:@{@"username":self.myUserNameTextField.text,@"password":self.myPasswordTextField.text,@"vcode":self.myRegisterCoderTextField.text}];
    [registerApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        if ([dict[@"errcode"] intValue] == 0) {
            [SVProgressHUD showSuccessWithStatus:dict[@"errmsg"]];
            if (self.updateBlock) {
                self.updateBlock(self.myUserNameTextField.text);
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [SVProgressHUD showErrorWithStatus:dict[@"errmsg"]];
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        [SVProgressHUD showErrorWithStatus:@"注册失败"];
    }];
}

//点击后时间倒数
- (void)VersionTimerout
{
    if (self.temptime == 0)
    {
        [self.TimerWithTimerout invalidate];
        self.TimerWithTimerout = nil;
        [self.registerCoderBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self.registerCoderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self.registerCoderBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        self.registerCoderBtn.enabled = YES;
        //        [self.sendCodeBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
        return;
    }
    self.temptime--;
    [self.registerCoderBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.registerCoderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.registerCoderBtn setTitle:[NSString stringWithFormat:@"%lds",(long)self.temptime] forState:UIControlStateNormal];
    self.registerCoderBtn.enabled = NO;
}

- (void)dealloc{

    NSLog(@"=============xiaohui================");
}
@end
